<?php
/**
 * 积分申请审批
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-29
 * Time: 下午8:51
 */

class PointsApprovalController extends Controller{

    public function index(){
        $this->getSmarty ();
        $this->smarty->display ("admin/pointsApproval.tpl" );
    }

    /**
     * 审评用户积分申请
     */
    public function approval(){
        require_once SERVICE.DS.'admin'.DS.'PointsApprovalService.class.php';

        $user = $_SESSION ['aduser'];
        $service = new PointsApprovalService($this->getDB ());
        $reply_userid = $user->id;
        $reply_usename =$user->user_name;

        $data = array(
            'reply_userid' => $reply_userid,
            'reply_username' => $reply_usename,
            'points' => $_POST['points'],
            'state' => $_POST['state'],
        );

        $id = $_POST["id"];
        $service->approval($data,$id);

        //审核通过，增加record
        if($_POST['state']==1){
            $record = array(
                'userid' => $_POST['userid'],
                'points' => $_POST['points'],
                'points_type' => 2,
                'points_desc' => $_POST['points_desc'],
                'record_type' => 0
            );
            $service->addUserPointsRecord($record);

            //邀请者积分增加10%
            $service->addInviteUserPointsRecord($_POST['points'],$_POST['userid']);
        }


        echo '1';
    }

    /**
     * 获取积分申请记录 分页
     */
    function getPointsApply(){
        require_once SERVICE.DS.'admin'.DS.'PointsApprovalService.class.php';
        $service = new PointsApprovalService($this->getDB());

        $page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
        $rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;

        $start = ($page - 1) * $rows;
        $result = array ();
        $condition = '';

        $row_count = $service->getPointsApplyCount ( $condition );
        $result ["total"] = $row_count;
        $resource_infos = $service-> getPointsApply($condition, $start, $rows);
        $result ["rows"] = $resource_infos;
        echo json_encode ( $result );
    }
}

?>